import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/index'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: () => import('@/views/login/LoginPage.vue') },
    {
      path: '/',
      component: () => import('@/views/layout/LayoutPage.vue'),
      redirect: '/about',
      children: [
        { path: '/about', component: () => import('@/views/about/About3G.vue') },
        { path: '/signup', component: () => import('@/views/signup/SignUpPage.vue') },
        { path: '/user', component: () => import('@/views/user/UserProfile.vue') }
      ]
    }
  ],
})


// 所有的路由在真正被访问到之前（解析渲染对应组件页面之前），都会先经过全局前置守卫
// 只有全局前置守卫放行了，才会到达相应的页面
// 定义一个数组，用来存放需要登陆权限的页面路径
const authUrl = ['/signup', '/user']
// 全局前置守卫
router.beforeEach((to) => {
  // 等待 store 初始化后再进行权限验证
  const userStore = useUserStore()
  // 权限页面判断是否有token
  if (!userStore.token) {
    if (to.path == '/signup' || to.path == '/user') {
      return '/login'
    }
  }
})

export default router
